
package cn.school.mybatis.filter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Date;

@WebFilter("/*")
public class TimeFliter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        System.out.println("过滤器初始化成功......");
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {

        long start=System.currentTimeMillis();
        HttpServletRequest req= (HttpServletRequest) servletRequest;
        req.setCharacterEncoding("utf-8");
        servletResponse.setContentType("utf-8");
        //访问地址
        String path=req.getServletPath();
        System.err.println("进入"+path+"的时间是："+new Date(start).toLocaleString());
        //放行
        filterChain.doFilter(servletRequest,servletResponse);
        long end=System.currentTimeMillis();
        System.err.println("离开"+path+"的时间是："+new Date(end).toLocaleString());
        System.err.println("共花费时间："+(end-start)+"微秒");

    }

    @Override
    public void destroy() {
        System.out.println("过滤器销毁成功......");
    }
}

